#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //去窗口边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    //把窗口背景设置为透明;
    setAttribute(Qt::WA_TranslucentBackground);

    //数据库配置
    db = QSqlDatabase::addDatabase("QODBC");
    db.setHostName("localhost");         // 服务器名称
    db.setDatabaseName("parking");       // 数据源名称
    db.setUserName("parking");               // 用户名
    db.setPassword("parking123");         // 密码

    //数据库连接
    if(db.open())
    {
        QMessageBox::information(this,"连接提示","连接成功");
    }
    else {
        QMessageBox::warning(this,"连接提示","连接失败");
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_x_clicked()
{
    close();
}

void Widget::on_signin_clicked()
{
    Register *is=new Register();
    is->show();
//    QString password = ui->PasswordEdit_2->text();
//    globalid = ui->IDEdit->text();
//    //对密码进行SM3加密
//    QCryptographicHash hash(QCryptographicHash::Sm3);
//    //放入要加密的数据
//    hash.addData(password.toUtf8());
//    //取出加密结果
//    QByteArray arr = hash.result();
//    password = arr.toHex();
//    qDebug()<<password;

//    //构造sql语句
//    QString sql = QString("insert into login(ID,Password) values('%1','%2')").arg(globalid).arg(password);

//    //执行sql语句
//    QSqlQuery query;
//    if(!query.exec(sql)){
//        QMessageBox::warning(this,"提示","注册失败");
//    }
//    else{
//        QMessageBox::information(this,"提示","注册成功");
//    }
}
QString globalid;
//登录
void Widget::on_login_clicked()
{
    QString password = ui->PasswordEdit_2->text();
    globalid = ui->IDEdit->text();

    //对密码进行SM3加密
    QCryptographicHash hash(QCryptographicHash::Sm3);
    //放入要加密的数据
    hash.addData(password.toUtf8());
    //取出加密结果
    QByteArray arr = hash.result();
    password = arr.toHex();
//    qDebug()<<password;

    //构造sql语句
    QString sql = QString("select * from login where ID='%1' and Password = '%2'").arg(globalid).arg(password);

    //执行sql语句
    QSqlQuery query;
    if(!query.exec(sql)){
        QMessageBox::warning(this,"提示","执行失败");
    }
    //登录成功
    if(query.next()){
       /* QMessageBox::information(this,"提示","登录成功")*/
//        this->close();
    //调转到information界面
        information *in=new information();
        in->show();
        this->close(); //?

    }
    else{
        QMessageBox::warning(this,"提示","登录失败");
    }
}






